package FlyGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginPanel extends JPanel {
    public JLabel unameLabel;
    public JLabel pwdLabel;
    public JTextField username;
    public JTextField password;
    public JButton ok;
    public DbUtil dbUtil;
    Object[] params;

    public LoginPanel() {
        unameLabel = new JLabel("用户名：");
        pwdLabel = new JLabel("密码：");
        username = new JTextField();
        password = new JTextField();
        ok = new JButton("登录");
        ok.addActionListener(new login());
        dbUtil = new DbUtil();

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.add(unameLabel);
        unameLabel.setBounds(50,80,60,30);
        this.add(pwdLabel);
        pwdLabel.setBounds(50,125,60,30);

        this.add(username);
        username.setBounds(120,80,200,25);

        this.add(password);
        password.setBounds(120,125,200,25);

        this.add(ok);
        ok.setBounds(180,180,70,30);


    }

    class login implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String uname = username.getText();
            String pwd = password.getText();
            params = new Object[]{uname};
            ResultSet resultSet = dbUtil.doQuery("select * from 2_com.student where username = ?", params);
            try {
                if (resultSet.next()) {
                    if(pwd.equals(resultSet.getString("password"))){
                        JOptionPane.showMessageDialog(null,"登录成功");
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"用户名或密码错误");
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null,"用户名或密码错误");
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
}
